<%@page import="com.proyecto.bl.Usuario"%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pagina principal</title>
        <link href="scripts/css.css" rel="stylesheet" type="text/css" />
        <link href="scripts/jquery.dataTables.css" rel="stylesheet" type="text/css" />
        <link href="scripts/demo_table.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="scripts/jquery.dataTables.min.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
                establecerNombre();
                
               $('#loginBtn').click(function(){
                   validate();
               }
               );
                   $('#logOut').click(function(){
                   logout();
               }
               );


                   $('.hide').hide();
              
                   
                
    });
  
    function initTable(){
       
        $.getJSON('ConsultarLibros?p1=4', 
                    function(data) {
                        
                        var cols= data;
                        
                        $('#dataTable').dataTable({
                           
                        
                        "bDestroy": true,
                        "sSearch":"Buscar",
                        "sPaginationType": "full_numbers",
                        "aoColumns":onLoadColumns(cols),
                        "oLanguage": {
			"sLengthMenu": "Mostrar _MENU_ registros por pagina.",
			"sZeroRecords": "No hay coincidencias.",
			"sInfo": "Mostrando _START_ - _END_ de _TOTAL_ registros.",
			"sInfoEmpty": "No hay registros.",
			"sInfoFiltered": "(Registros filtrados de _MAX_ registros totales.)"
                        }
                        });    
                      buscar();
                    });
    }
                 function buscar(){
                 var url = 'ConsultarLibros?p1=5';
                 
                    $.getJSON(url, 
                    function(data) {
                        if(data){
                        $('#dataTable').dataTable().fnClearTable();
                        $('#dataTable').dataTable().fnAddData(data);
                        $('#dataTable').dataTable().fnDraw();
                        $(':button.btnCancelarRes').click(
                            function(e) {               
                              var idSolicitud = $(e.currentTarget).attr('id');
                               cancelarReserva(idSolicitud);
                            });
                        }else{
                             $('#dataTable').dataTable().fnClearTable();
                            $('#dataTable').dataTable().fnDraw();
                        }
                            
                    });
                 }
                 
                 
                 function cancelarReserva(idSolicitud){
                    $.getJSON('ConsultarLibros?p1=7&p2='+idSolicitud,
                       function (data){
                            if(data){
                                 buscar();
                            }
                       }
                     );
                 }
                function establecerNombre(){
                    $('body').hide();
                    $.getJSON('LoginServlet?p3=2', 
                    function(data) {
                         if(data!=null){
                         $('#bienvenido').html("Bienvenido, "+"<a href='usuario.jsp'>"+data.nombre+"</a>");
                         $('.hide').show();
                         $('body').show();
                         initTable();

                        }else
                         {
                            window.location = "index.jsp";
                         }


                        });
                }
                function logout(){
                    $.getJSON('LoginServlet?p3=3', 
                    function() {
                        window.location = "index.jsp";
                        });
                }
                function onLoadColumns(cols) {
                    var columns = [];
                    for (var i = 0; i < cols.length; i++) {
                        columns[i] = { sTitle: cols[i] };
                    }

                    return columns;
                }
</script>
</head>

<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#FFFFFF"><table width="1000" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="83" valign="top" background="images/top-bg.gif" style="padding-top:51px;">
		<table width="284" border="0" align="left" cellpadding="0" cellspacing="0">
          <tr>
        
          </tr>
          <tr>
        
          </tr>
        </table></td>
        <td width="472" background="images/top-bg.gif"><img src="images/logo.png" width="250" height="134" /></td>
      </tr>
    </table>
      <table width="1000" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td id="button-bg">
		 <ul>
		<li><a href="index.jsp">HOME</a></li>
		<li><a href="consulta.jsp">CONSULTA</a></li>
                <li ><a href="usuario.jsp">MIS DATOS</a></li>
                <li ><a href="reservas.jsp">MIS RESERVAS</a></li>
		</ul>
		  </td>
        </tr>
      </table>
<div id="login" style="float:right;">
<div id="bienvenido">   </div>  
<a href="#" id="logOut" class="hide" style="float:right;">  Salir </a>
</div>
      <table id="reservas">
		    <form name="formulario">
              <table border="0" cellspacing="2" cellpadding="2" align="center">
                 <center>
				<h2 >Mis reservas</h2>
                 

                
              </table>
                        
<table cellpadding="0" cellspacing="0" border="0" class="display" id="dataTable">
	<thead>
		<tr>


		</tr>
	</thead>
	<tbody>
		
	</tbody>

</table>
              <br>
		
          </form>
		 
	 </table>
	 </center>
     
   </td>
  </tr>
</table>
    
    </body>
</html>

